package com.woniuxy.microfundservice.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.woniuxy.bean.ResultBean;
import com.woniuxy.microfundservice.bean.info.FundPosition;
import com.woniuxy.microfundservice.bean.info.FundPositionMsg;
import com.woniuxy.microfundservice.dao.FundPositionDao;
import com.woniuxy.microfundservice.service.FundPositionService;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

/**
 * (FundPosition)表服务实现类
 *
 * @author makejava
 * @since 2021-04-08 09:40:40
 */
@Service("fundPositionService")
public class FundPositionServiceImpl implements FundPositionService {
    @Resource
    private FundPositionDao fundPositionDao;

    @Resource
    private RestTemplate restTemplate;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public FundPosition queryById(Integer id) {
        return this.fundPositionDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<FundPosition> queryAllByLimit(int offset, int limit) {
        return this.fundPositionDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param fundPosition 实例对象
     * @return 实例对象
     */
    @Override
    public FundPosition insert(FundPosition fundPosition) {
        this.fundPositionDao.insert(fundPosition);
        return fundPosition;
    }

    /**
     * 修改数据
     *
     * @param fundPosition 实例对象
     * @return 实例对象
     */
    @Override
    public FundPosition update(FundPosition fundPosition) {
        this.fundPositionDao.update(fundPosition);
        return this.queryById(fundPosition.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.fundPositionDao.deleteById(id) > 0;
    }

    @Override
    public FundPosition selectLocal(Integer fundId) {
        return fundPositionDao.selectLocal(fundId);
    }

    @Override
    public int addOne(FundPosition fundPosition) {
        return fundPositionDao.addOne(fundPosition);
    }

    @Override
    public ResultBean selectAdd(int fundCodeInt) {
        String fundCode=String.format("%06d",fundCodeInt);
        HttpHeaders httpHeaders = new HttpHeaders();
        //定义传输的数据的数据方式
        //组装数据
        MultiValueMap<String, String> map= new LinkedMultiValueMap<>();
        HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(map, httpHeaders);
        //外部接口的地址
        String url="https://api.doctorxiong.club/v1/fund/position?code="+fundCode;
        ResponseEntity<FundPositionMsg> exchange = restTemplate.exchange(url, HttpMethod.GET, httpEntity, FundPositionMsg.class);
        //取出持仓对象
        FundPosition fundPosition = exchange.getBody().getData();
        int i = Integer.parseInt(fundCode);
        //关联基金code
        fundPosition.setFundCode(i);
        //存进数据库
        addOne(fundPosition);
        return ResultBean.builder().code(200).data(exchange.getBody().getData()).build();
    }
}